from django.shortcuts import render
from demo import models
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from json import dumps
from django.http import HttpResponse
from django.db.models import Q
from django.db import connection
# Create your views here.

'''初始化页面'''
def index(request):
    _echartsList = [
        {'name': '蒸发量', 'data': [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]},
        {'name': '降水量', 'data': [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]},
        {'name': '平均温度', 'data': [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]}
    ]
    maps = models.Teacher.objects.all()
    paginator = Paginator(maps, 3, 2)
    page = request.GET.get('page')
    try:
        _m = paginator.page(page)
    except PageNotAnInteger:
        _m = paginator.page(1)
    except EmptyPage:
        _m = paginator.page(paginator.num_pages)
    one = models.Teacher.objects.get(no='1')
    two = models.Teacher.objects.get(no='2')
    ctx = {'map': _m, 'one': one, 'two': two, 'list': dumps(_echartsList)}
    return render(request, 'demo/index.html', ctx)

'''检索框'''
def index_search(request):
    # ctx = {'code': 200}
    if request.method == 'GET':
        pass
    else:
        u_name = request.POST.get('u_name', '')
        long_name = request.POST.get('long_name', '')
        try:
            list = []
            if u_name.strip():
                teacher = models.Teacher.objects.filter(Q(name__contains=u_name))
                print(teacher)
                list= qset_parent_list(teacher, list, 0)
            if long_name.strip():
                teacher_two = models.Teacher.objects.filter(Q(name__contains=long_name))
                print(teacher_two)
                list=qset_parent_list(teacher_two, list, 1)
        except models.Teacher.DoesNotExist:
            pass
        # 中文乱码
        return HttpResponse(dumps(list, ensure_ascii=False))


'''根据状态符号装填字段'''
def qset_parent_list(obj,list,zt):
    for inx, y in enumerate(obj):
        if inx == 0:
            list.append({
                "zt": zt, # 根据状态判断search的第几行数据
                "no": y.no,
                "name": y.name,
                "intro": y.intro,
                "motto": y.motto,
                "photo": y.photo,
                "manager": y.manager
            })
    return list

'''page分页'''
def listPage(request):
    maps = models.Teacher.objects.all()
    paginator = Paginator(maps, 3, 2)
    page = request.GET.get('page')
    try:
        _m = paginator.page(page)
    except PageNotAnInteger:
        _m = paginator.page(1)
    except EmptyPage:
        _m = paginator.page(paginator.num_pages)
    num = paginator.num_pages
    list = [{'page': page, 'range': num}]
    for i in range(len(_m)):
        list.append({
            "no": _m[i].no,
            "name": _m[i].name,
            "intro": _m[i].intro,
            "motto": _m[i].motto,
            "photo": _m[i].photo,
            "manager": _m[i].manager
        })
    return HttpResponse(dumps(list, ensure_ascii=False))